/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cassys.ejbs;

import cassys.application.DTOs.UserDTO;
import cassys.application.services.UserManager;
import javax.ejb.Stateless;
import cassys.data.DAOFactory;
import cassys.data.UserDAO;
import cassys.domain.user.User;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

/**
 *
 * @author UserRoleMapping
 */
@Stateless(mappedName = "UserController")
@TransactionManagement(TransactionManagementType.CONTAINER)
public class UserManagerBean implements UserManager {

    @Resource
    private SessionContext context;
    DAOFactory daoFactory = DAOFactory.instance(DAOFactory.HIBERNATE);

    @Override
    public List<User> getUser(String userId) throws Exception {
        UserDAO userRepository = daoFactory.getUserDAO();
        return userRepository.getUser(userId);
    }

    @Override
    public void saveUser(UserDTO user) throws Exception {
        UserDAO userRepository = daoFactory.getUserDAO();
        User newUser = adaptDTO(user);
        userRepository.saveUser(newUser);
    }

    private User adaptDTO(UserDTO user) {
        User newUser = new User();
        newUser.setCity(user.getCity());
        newUser.setContactNumber(user.getContactNumber());
        newUser.setEmail(user.getEmail());
        newUser.setFirstName(user.getFirstName());
        newUser.setLastName(user.getLastName());
        newUser.setPassword(user.getPassword());
        newUser.setPincode(user.getPincode());
        newUser.setStreetName(user.getStreetName());
        newUser.setUserId(user.getUserId());
        return newUser;
    }

    @Override
    public void uploadCSV(UserDTO user) throws Exception {
        UserDAO userRepository = daoFactory.getUserDAO();
        User uploadedUser = adaptDTO(user);
        userRepository.uploadCSV(uploadedUser);
    }

    @Override
    public void modifyUser(User user, String role) throws Exception {
        UserDAO userRepository = daoFactory.getUserDAO();
        userRepository.modifyUser(user, role);
    }

    @Override
    public void deleteUser(int userSeqNo) throws Exception {
        UserDAO userRepository = daoFactory.getUserDAO();
        userRepository.deleteUser(userSeqNo);
    }
}
